<?php
/*
claimid=hospitalcode+membernumber+dateadmitted+timeadmitted

hospitalcode(######)6     
membernumber(##-#########-#)14
dateadmitted(yyyymmdd)8
timeadmitted(HHmm)6
*/
class PatientsController extends AppController{
	function setRefiled($patientid){		
		$this->data['Patient']['id']=$patientid;
		$this->data['Patient']['isrefiled']=1;
		
		$this->Patient->save($this->data);
	}
	function getFinalDiagnosis($patientid){
		$this->view='Json';
		
		$condition=array('id'=>$patientid);
		$field=array('finaldiagnosis');
		$data=$this->Patient->find('all',array('conditions'=>$condition, 'fields'=>$field));			
												
		$data[]=$this->requestAction('/casetypes/getCaseType_/'.$data[0]['Patient']['finaldiagnosis']);
		//var_dump($data);
		$this->set('data', $data);
		$this->set('json', 'data');
	}
	function updateFinalDiagnosis(){
		//data: 'id='+patid+'&index='+index+'&icdcode='+icdcode,
		$id=$_POST['id'];
		$index=$_POST['index'];
		$icdcode=$_POST['icdcode'];
		
		$data=$this->Patient->findById($id);				 
		$diagnosis=explode(';', $data['Patient']['finaldiagnosis']);
		$diagnosis[$index]=$icdcode;
		$fd="";
		foreach($diagnosis as $icd){
			$fd.=$icd.";";
		}
		$fd=substr($fd, 0, strlen($fd)-1);
		$this->data['Patient']['id']=$id;
		$this->data['Patient']['finaldiagnosis']=$fd;
		$this->Patient->save($this->data);
	}	
	function index(){
		$data=null;
		if(!empty($this->data)){
			if($this->data['Patient']['searyby']=="Hospital Code"){
				$data=$this->Patient->find('all', 
					array('conditions'=>array('hospitalnumber like'=>$this->data['Patient']['keyword']."%"))
				);								
			}
			else if($this->data['Patient']['searyby']=="Patient Name"){
				$data=$this->Patient->find('all', 
						array('conditions'=>array(
							'OR'=>array('patientlastname LIKE'=>$this->data['Patient']['keyword']."%",
								'patientmiddlename LIKE'=>$this->data['Patient']['keyword']."%",
								'patientfirstname LIKE'=>$this->data['Patient']['keyword']."%"
							)
						)
					)
				);			
			}
			else if($this->data['Patient']['searyby']=="PhilHealth Number"){
				$data=$this->Patient->find('all',
					array('conditions'=>array(
						'membernumber LIKE'=>$this->data['Patient']['keyword']."%"	
						)
					)
				);
			}
			else{$data=$this->Patient->findAll();}
		}
		else{
			$data=$this->Patient->findAll();	
		}		
		$this->set('data',$data);		
		$this->set('i',1);		
	}
	function insert($data){			
		$this->data['Patient']['accreditation']=$data[0];
		$this->data['Patient']['category']=$data[1];
		$this->data['Patient']['hospitalname']=$data[2];
		$this->data['Patient']['hospitalnumber']=$data[3];
		$this->data['Patient']['hospitalstreet']=$data[4];
		$this->data['Patient']['hospitalbarangay']=$data[5];
		$this->data['Patient']['hospitalcity']=$data[6];
		$this->data['Patient']['hospitalprovince']=$data[7];
		$this->data['Patient']['hospitalzipcode']=$data[8];		
		$this->data['Patient']['memberlastname']=$data[9];
		$this->data['Patient']['membermiddlename']=$data[10];
		$this->data['Patient']['memberfirstname']=$data[11];				
		$this->data['Patient']['membernumber']=$data[12];
		$this->data['Patient']['memberstreet']=$data[13];
		$this->data['Patient']['memberbarangay']=$data[14];
		$this->data['Patient']['membercity']=$data[15];
		$this->data['Patient']['memberprovince']=$data[16];
		$this->data['Patient']['memberzipcode']=$data[17];
		$this->data['Patient']['patientfirstname']=$data[18];
		$this->data['Patient']['patientlastname']=$data[19];
		$this->data['Patient']['patientmiddlename']=$data[20];
		$this->data['Patient']['patientgender']=$data[21];
		$this->data['Patient']['patientage']=$data[22];		
		$this->data['Patient']['admissiondiagnosis']=$data[23];
		$this->data['Patient']['dateadmitted']=date('Y-m-d',strtotime($this->toDate($data[24])));
		$this->data['Patient']['timeadmitted']=date('H:i:s',strtotime($this->toTime($data[25])));
		$this->data['Patient']['datedischarged']=date('Y-m-d',strtotime($this->toDate($data[26])));
		$this->data['Patient']['timedischarged']=date('H:i:s',strtotime($this->toTime($data[27])));
		$this->data['Patient']['numberday']=$data[28];
		$this->data['Patient']['datedeath']=date('Y-m-d',strtotime($this->toDate($data[29])));
		$this->data['Patient']['isevalute']=0;

		$this->Patient->create();
		$this->Patient->save($this->data);					
	}
	private function toDate($date){
		$y=substr($date,4,4);
		$m=substr($date,0,2);
		$d=substr($date,2,2);
		$date=$y.'-'.$m.'-'.$d;		
		return $date;
	}	
	private function toTime($time){
		$h=substr($time,0,2);
		$m=substr($time,2,2);
		$time=$h.':'.$m;
		return $time;
	}
    function printEvaluate($claimid)
   	{       	
		
        
    }	
}
?>